package com.org.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.org.bean.setting.DepartInfo;
import com.org.service.DepartService;

@Controller
@RequestMapping("/depart")
public class DepartAction {
	/**
	 * 部门信息初始化，录入及展示
	 */
	@Autowired
	private DepartService departService;
	private Iterator<String> iter;
	private List<String> list;

	private Map<String, Object> status(List<DepartInfo> departInfo) { // 封装成一个Json文件返回
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (departInfo.size() != 0) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
			m.put("result", departInfo);
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");
		}
		return m;
	}

	private Map<String, Object> status(Boolean departInfo) {
		// TODO Auto-generated method stub
		Map<String, Object> m = new HashMap<String, Object>();
		if (departInfo) {
			m.put("statusCode", 1);
			m.put("msg", "成功");
		} else {
			m.put("statusCode", 0);
			m.put("msg", "失败");
		}
		return m;
	}

	@RequestMapping("/findAll")
	public @ResponseBody Map<String, Object> getDepartInfos() {
		List<DepartInfo> departInfo = new ArrayList<DepartInfo>();
		departInfo = departService.findAll();
		return status(departInfo);
	}

	@RequestMapping("{departId}/findById")
	public @ResponseBody Map<String, Object> getDepartInfo(
			@PathVariable int departId) {
		List<DepartInfo> departInfo = new ArrayList<DepartInfo>();
		departInfo = departService.findById(departId);
		return status(departInfo);

	}

	@RequestMapping("{regionId}/findByRegion")
	public @ResponseBody Map<String, Object> getDeparNametInfo(
			@PathVariable int regionId) {
		List<DepartInfo> departInfo = new ArrayList<DepartInfo>();
		departInfo = departService.findByRegion(regionId);
		return status(departInfo);

	}

	@RequestMapping("/addDepart")
	public @ResponseBody Map<String, Object> addDepart(
			@RequestBody Map<String, Object> departMap) {
		boolean result;
		iter = departMap.keySet().iterator();
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		DepartInfo departInfo = new DepartInfo();
		departInfo.setPersonId((Integer) departMap.get(list.get(0)));
		departInfo.setRegionId((Integer) departMap.get(list.get(1)));
		departInfo.setDepartName((String) departMap.get(list.get(2)));
		departInfo.setType((String) departMap.get(list.get(3)));
		departInfo.setRemark((String) departMap.get(list.get(4)));
		result = departService.addDepart(departInfo);
		return status(result);
	}

	@RequestMapping("/updateDepart")
	public @ResponseBody Map<String, Object> updateDepart(
			@RequestBody Map<String, Object> departMap) {
		boolean result;
		iter = departMap.keySet().iterator(); // 获取Map集合的key值
		list = new ArrayList<String>();
		while (iter.hasNext()) {
			list.add(iter.next());
		}
		DepartInfo departInfo = new DepartInfo();
		departInfo.setDepartId((Integer) departMap.get(list.get(0)));
		departInfo.setPersonId((Integer) departMap.get(list.get(1)));
		departInfo.setRegionId((Integer) departMap.get(list.get(2)));
		departInfo.setDepartName((String) departMap.get(list.get(3)));
		departInfo.setType((String) departMap.get(list.get(4)));
		departInfo.setRemark((String) departMap.get(list.get(5)));
		result = departService.updateDepart(departInfo);
		return status(result);
	}

	@RequestMapping("/{departId}/deleteDepart")
	public @ResponseBody Map<String, Object> deleteDepart(
			@PathVariable int departId) {
		boolean result;
		result = departService.deleteDepart(departId);
		return status(result);

	}
}
